<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
    <!-- 调试信息 -->
    <div class="hidden">
        <p>当前选中项目 ID: {$selected_project}</p>
        {if condition="isset($debug_info)"}
        <pre>{:var_export($debug_info, true)}</pre>
        {/if}
    </div>
    <script>
    // 在页面加载完成后与父窗口交互
    window.onload = function() {
        try {
            // 尝试获取父窗口选中的项目 ID
            if (window.parent && window.parent.$("#project-select").length > 0) {
                var parentSelectedProject = window.parent.$("#project-select").val();
                console.log('Parent selected project ID: ' + parentSelectedProject);
                
                if (parentSelectedProject && parentSelectedProject != '0') {
                    // 设置当前窗口的项目选择框
                    var select = document.getElementById('c-publicwelfare_id');
                    if (select) {
                        select.value = parentSelectedProject;
                        $(select).trigger('change');
                        console.log('Set project ID from parent window: ' + parentSelectedProject);
                    }
                }
            } else {
                // 如果无法与父窗口交互，则使用控制器传递的值
                var selectedProject = '{$selected_project}';
                if (selectedProject && selectedProject != '0') {
                    var select = document.getElementById('c-publicwelfare_id');
                    if (select) {
                        select.value = selectedProject;
                        $(select).trigger('change');
                        console.log('Setting project ID from controller: ' + selectedProject);
                    }
                }
            }
        } catch (e) {
            console.error('Error interacting with parent window: ', e);
            // 出错时使用控制器传递的值
            var selectedProject = '{$selected_project}';
            if (selectedProject && selectedProject != '0') {
                var select = document.getElementById('c-publicwelfare_id');
                if (select) {
                    select.value = selectedProject;
                    $(select).trigger('change');
                    console.log('Setting project ID from controller (fallback): ' + selectedProject);
                }
            }
        }
    };
    </script>
    
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Project_name')}:</label>
        <div class="col-xs-12 col-sm-8">
            <select id="c-publicwelfare_id" class="form-control selectpicker" name="row[publicwelfare_id]" data-rule="required">
                <option value="">请选择</option>
                {foreach name="projects" item="vo"}
                <option value="{$vo.publicwelfare_id}" {if condition="$vo.publicwelfare_id == $selected_project"}selected{/if}>{$vo.project_name} ({$vo.district_name})</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Course_name')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-course_name" class="form-control" name="row[course_name]" type="text" value="" data-rule="required">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">主讲单位:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-teaching_unit" class="form-control" name="row[teaching_unit]" type="text" value="" placeholder="请输入主讲单位">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">课程图片:</label>
        <div class="col-xs-12 col-sm-8">
            <div class="input-group">
                <input id="c-course_image" class="form-control" name="row[course_image]" type="text" value="" placeholder="请选择或上传课程图片">
                <div class="input-group-btn">
                    <button type="button" id="plupload-course-image" class="btn btn-danger plupload" data-input-id="c-course_image" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-course-image"><i class="fa fa-upload"></i> {:__('Upload')}</button>
                    <button type="button" class="btn btn-success ai-generate-image" data-type="ai-generate-course_image" data-input-id="c-course_image" data-width="1024" data-height="1024" data-say="为这个课程生成一张有创意的封面图片，风格现代简洁，色彩温暖" title="AI生成图片"><i class="fa fa-magic"></i> AI生成</button>
                </div>
            </div>
            <span class="msg-box n-right" for="c-course_image"></span>
            <div class="form-inline">
                <img id="p-course-image" class="img-responsive img-thumbnail plupload-preview" src="" style="max-height:150px;" />
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">课程介绍:</label>
        <div class="col-xs-12 col-sm-8">
            <div class="input-group">
                <textarea id="c-course_description" class="form-control" name="row[course_description]" rows="4" placeholder="请输入课程介绍"></textarea>
                <div class="input-group-btn" style="vertical-align: top;">
                    <button type="button" class="btn btn-success" data-ai-generate-text data-target-id="c-course_description" data-prompt-template="为课程'{name}'生成简单的课程介绍以普通话形式输出，最多不超过200字。" data-max-length="1000" data-say="" data-size="1024x1024" data-preview-id="p-course-description" title="AI生成介绍"><i class="fa fa-magic"></i> AI生成</button>
                </div>
            </div>
            <span class="msg-box n-right" for="c-course_description"></span>
        </div>
    </div>
    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <button type="submit" class="btn btn-success btn-embossed">{:__('OK')}</button>
            <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
        </div>
    </div>
</form>
